<?php

class User_model extends BZ_Model
{
    protected $table = 'user';
    private $table_2 = 'user_account';
    private $table_4 = 'sales_order';

    public function findByAutocomplete()
    {
        $this->db->select('uid, user_name, alias_name');
        return $this->db->get($this->table);
    }

    public function findByIds($data)
    {
        if ( isset($data['uid']) ) {
            if ( is_array($data['uid']) ) {
                $this->db->where_in('uid', $data['uid']);
            } else {
                $this->db->where('uid', $data['uid']);
            }
        }
        if ( isset($data['user_name']) ) {
            $this->db->where_in('user_name', $data['user_name']);
        }
        if ( isset($data['user_type']) ) {
            $this->db->where('user_type & ' . $data['user_type'] . '=', $data['user_type']);
        }

        return $this->db->get($this->table);
    }

    public function findById($uid)
    {
        $this->db->where('uid', $uid);
        $this->db->limit(1);

        return $this->db->get($this->table);
    }

    public function findByUid($uid)
    {
        $this->db->select('user.uid,user.user_name,user.alias_name,user.user_type');
        $this->db->from($this->table . ' as user');
        //$this->db->join($this->table_2.' as user_account', 'user_account.uid = user.uid');
        //$this->db->join($this->table_3 . ' as user_detail', 'user_detail.uid = user.uid');
        $this->db->where('user.uid', $uid);
        $this->db->limit(1);

        return $this->db->get();
    }

    public function validateAccountPassword($uid, $password)
    {
        $this->db->select('*');
        $this->db->from($this->table . ' as user');
        $this->db->join($this->table_2 . ' as user_account', 'user_account.uid = user.uid');
        $this->db->where('user.uid', $uid);
        $this->db->where('user.pw', md5($password));

        return $this->db->get();
    }

    public function checkParentsByUsername($username)
    {
        $this->db->select('*');
        $this->db->from($this->table . ' as user');
        $this->db->where('user_name', $username);
        $this->db->where('user_type &' . UTID_SELLER . '=', UTID_SELLER);

        return $this->db->get();
    }

    public function login($postData)
    {
        $username = trim($postData['user_name']);
        $this->db->where("(`user_name`='{$username}' OR `mobile_phone`='{$username}')");
        $this->db->where('pw', md5($postData['password']));

        //$this->db->where('user_type & '.UTID_PROVIDER.'!=', UTID_PROVIDER);//非供应商帐号登录
        return $this->db->get($this->table);
    }

    public function visitCount($uid)
    {
        $this->db->set('login_count', 'login_count+1', false);
        $this->db->where('uid', $uid);

        return $this->db->update($this->table);
    }

    /**
     * 验证用户名
     * @param $userName
     * @return mixed
     */
    public function validateName($userName)
    {
        $this->db->where('user_name', $userName);

        return $this->db->get($this->table);
    }

    /**
     * 验证手机号码与用户名
     * @param $Phone
     * @return mixed
     */
    public function validateMobilePhone($Phone)
    {
        $this->db->where('user_name', $Phone);
        $this->db->or_where('mobile_phone', $Phone);

        return $this->db->get($this->table);
    }

    /**
     * 验证用户名称
     * @param $user_name
     * @return mixed
     */
    public function validateExistUser($user_name)
    {
        $this->db->where('user_name', $user_name);
        $this->db->where('user_type &' . UTID_SELLER . '=', UTID_SELLER);

        return $this->db->count_all_results($this->table);
    }

    /**
     * 手机号码是否存在
     * @param $mobilePhone
     * @return mixed
     */
    public function findByMobilePhone($mobilePhone)
    {
        $this->db->select('user.uid');
        $this->db->from($this->table . ' as user');
        $this->db->where('user.user_name', $mobilePhone);
        $this->db->or_where('mobile_phone', $mobilePhone);
        $this->db->limit(1);

        return $this->db->get();
    }

    /**
     * 查询用户信息
     */
    public function findUserInfo($uid)
    {
        $this->db->select('user.uid,user.user_name,user.alias_name,personal_photo,amount_carry, amount_bonus, mobile_phone');
        $this->db->from($this->table . ' AS user');
        $this->db->join($this->table_2 . ' AS user_account', 'user.uid = user_account.uid', 'INNER');
        $this->db->where('user.uid', $uid);
        $this->db->limit(1);

        return $this->db->get();
    }

    /**
     * 更新用户基本信息
     */
    public function updateUserInfo($data, $uid)
    {
        $this->db->where('uid', $uid);

        return $this->db->update($this->table, $data);
    }

    /**
     * 获取上级用户的uid
     * @param $uid
     * @return mixed
     */
    public function getParentInforByUid($uid)
    {
        $this->db->select('uid,parent_id');
        $this->db->from($this->table);
        $this->db->where('uid', $uid);

        return $this->db->get();
    }


    /**
     * 验证支付密码是否正确
     */
    public function validatePayPw($uid, $pay_pw)
    {
        $this->db->where('uid', $uid);
        $this->db->where('pw', md5($pay_pw));

        return $this->db->get($this->table);
    }


    /**
     * @descripte   分润
     * @Author xiumao
     * @date 2016/7/7 0007 下午 7:02
     */
    public function page_list($page_num, $num, $search_param = array())
    {
        if ( !empty($search_param['parent_id']) ) {//下级会员
            $this->db->where('parent_id', $search_param['parent_id']);
        }
        if ( !empty($search_param['search']) ) {
            $search = trim(addslashes($search_param['search']));
            $where = "( (uid like '%{$search}%') or (user_name like '%{$search}%') or (alias_name like '%{$search}%') or (mobile_phone like '%{$search}%') )";
            $this->db->where($where);
        }
        $this->db->order_by('created_at', 'DESC');
        $this->db->limit($page_num, $num);

        return $this->db->get($this->table);
    }

    /**
     * 通过供应商uid查找订单数
     * @param $uid
     * @return mixed
     */
    public function getSalesOrderCountByUid($uid)
    {
        $this->db->from($this->table);
        $this->db->join($this->table_4 . ' AS sales_order', 'sales_order.uid=user.uid');
        $this->db->where('user.uid', $uid);
        $this->db->where('user.flag', 1);//没有冻结
        return $this->db->count_all_results();
    }

    public function get_data($w=array(), $f='*')
    {
        $this->db->select($f);
        $this->check_where($w);
        return $this->db->get($this->table);
    }

    private function check_where($w=array())
    {
        if (!empty($w['user'])) {
            $this->db->where("(`user_name`='{$w['user']}' OR `mobile_phone`='{$w['user']}')");
        }
        if (!empty($w['password'])) {
            $this->db->where('pw', $w['password']);
        }
        if (!empty($w['client_time'])) {
            $this->db->where('client_time', $w['client_time']);
        }
    }

    //判断是否注册
    public function hasRegister($mobile_phone)
    {
        $this->db->select('*');
        $this->db->where('user_name', $mobile_phone);
        $this->db->or_where('mobile_phone', $mobile_phone);
        return $this->db->get($this->table);
    }

    /**
     * 更新用户信息
     * @param array $data
     * @param array $where
     * @return bool
     */
    public function editUser($data=array(), $where=array())
    {
        $_data = array();
        if (!$where) {
            return false;
        }
        if (!empty($data['mobile_phone'])) {
            $_data['mobile_phone'] = $data['mobile_phone'];
        }
        if (!empty($data['user_email'])) {
            $_data['user_email'] = $data['user_email'];
        }
        if (!empty($data['pw'])) {
            $_data['pw'] = md5($data['pw']);
        }
        if (!empty($data['payment_password'])) {
            $_data['payment_password'] = md5($data['payment_password']);
        }
        if (!empty($data['client_time'])) {
            $_data['client_time'] = $data['client_time'];
        }
        $this->db->where($where);
        return $this->db->update($this->table, $_data);
    }

    /**
     * 注册时保存数据
     * @param array $postData
     * @param int $parent_id
     * @return mixed
     */
    public function insertUser($postData = [], $parent_id = UTID_BEIZHU)
    {
        $data = [
            'mobile_phone'   => '',
            'pw'             => '',
            'user_type'      => UTID_CUSTOMER,
            'parent_id'      => $parent_id,
            'owner_id'       => $parent_id,
            'sms'            => 0,
            'login_count'    => 1,
            'created_at'     => date('Y-m-d H:i:s'),
        ];
        if (!empty($postData['mobile_phone'])) {
            $data['mobile_phone'] = $postData['mobile_phone'];
        }
        if (!empty($postData['user_name'])) {
            $data['user_name']= trim($postData['user_name']);
        }
        if (!empty($postData['alias_name'])) {
            $data['alias_name'] = trim($postData['alias_name']);
        }
        if (!empty($postData['password'])) {
            $data['pw'] = md5($postData['password']);
        }
        if (!empty($data['payment_password'])) {
            $data['payment_password'] = md5($postData['payment_password']);
        }
        if (!empty($postData['user_type'])) {
            $data['user_type'] = $postData['user_type'];
        }
        if (!empty($postData['personal_photo'])) {
            $data['personal_photo'] = $postData['personal_photo'];
        }
        if (isset($postData['sex_id'])) {
            $data['sex_id'] = $postData['sex_id'];
        }
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    public function modifyPassword($postData=array())
    {
        $data = array(
            'pw' => md5($postData['password']),
        );
        $this->db->where('user_name', $postData['username']);
        return $this->db->update($this->table, $data);
    }

    public function updateUser($uid, $cellphone)
    {
        $data = array(
            'mobile_phone' => $cellphone,
        );
        $this->db->where('uid', $uid);
        $this->db->update($this->table, $data);
    }

    //验证用户名是否已存在
    public function findByUserName($postData)
    {
        if (!empty($postData['user_name'])) {
            $this->db->where('user_name', $postData['user_name']);
            if (!empty($postData['alias_name'])) {
                $this->db->or_where('alias_name', $postData['alias_name']);
            }
        }
        return $this->db->get($this->table);
    }
}